﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using N2;
using N2.Details;
using N2.Integrity;

namespace Dinamico.Models
{
    
    [PageDefinition("Redirect-Link",
    Description = "Create a redirection to other page",
    SortOrder = 4,
    IconUrl = "~/upload/img/icons/redirect.png")]
    [WithEditableTitle]
    [WithEditableName(ContainerName = Defaults.Containers.Metadata)]
    [WithEditableVisibility(ContainerName = Defaults.Containers.Metadata)]
    public class RedirectToFirstChildModel : ContentItem
    {
        //public override string Url
        //{
        //    get
        //    {
        //        return base.Url;
        //    }
        //}

        [EditableUrl("Url set by Operator", 100)]
        public virtual string UrlOperator
        {
            get
            {
                try
                {
                    return (string)(GetDetail("UrlOperator") ?? string.Empty);
                }
                catch (Exception ee)
                {
                    return string.Empty;
                }
            }
            set { SetDetail("UrlOperator", value, string.Empty); }
        }

        public string RedirectUrl
        {

            get
            {
                if (!String.IsNullOrEmpty(UrlOperator))
                    return UrlOperator;

                BaseDescribePage firstChild = this
                    .Children
                    .FirstOrDefault(x => x is Models.BaseDescribePage) as Models.BaseDescribePage;

                if (firstChild != null) return firstChild.Url;

                return String.Empty;
            }
        }




        [EditableCheckBox("Show this page in menu?", 1)]
        public virtual bool Show { get; set; }
    }
}